package com.tsingsoft.mail.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.tsingsoft.common.dao.DBAccess;
import com.tsingsoft.entity.EmailPool;
import com.tsingsoft.mail.dao.EmailPoolDao;

public class EmailPoolDaoImpl implements EmailPoolDao {

	@Override
	public boolean save(EmailPool ep) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		DBAccess dao = null;
		String strSql = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			strSql = "INSERT INTO " + EmailPool.FC_EMAIL_POOL+ " (" 
			+ EmailPool.ID + "," + EmailPool.BIZTYPE + ","
			+ EmailPool.MAILSUBJECT + "," + EmailPool.ATTACHMENTNAME +","
			+ EmailPool.ATTACHMENTPATH + "," + EmailPool.MAILCONTENT +","
			+ EmailPool.MAILSENDSTATUS + "," + EmailPool.CREATETIME + ","
			+ EmailPool.MARK
			+ ") VALUES ('"
			+ ep.getId() + "','" + ep.getBizType() + "','"
			+ ep.getMailSubject()+ "','" + ep.getAttachmentName()+"','"
			+ ep.getAttachmentPath()+ "','" + ep.getMailContent()+"','"
			+ ep.getMailSendStatus() + "',str_to_date('" + sdf.format(ep.getCreateTime())+ "','%Y-%m-%d %H:%i:%s'),'"
			+ ep.getMark()
			+ "')";	
			pstmt = conn.prepareStatement(strSql);
			if(pstmt.executeUpdate()<=0){
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dao.close(pstmt, conn);
		}
		return true;
	}

	@Override
	public boolean deleteById(EmailPool ep) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		DBAccess dao = null;
		String strSql = "";
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			strSql = "DELETE FROM " + EmailPool.FC_EMAIL_POOL
			+ "  where " +EmailPool.ID + "='" +ep.getId() + "'";
			pstmt = conn.prepareStatement(strSql);
			if(pstmt.executeUpdate() <= 0){
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dao.close(pstmt, conn);
		}
		return true;
	}

	@Override
	public List<EmailPool> findById(EmailPool ep) {
		DBAccess dao = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT * FROM " +  EmailPool.FC_EMAIL_POOL 
					+ " where " +EmailPool.ID +" ='" +ep.getId() +"'";
		try {
			List<EmailPool> arrData = new ArrayList<EmailPool>();
			dao = new DBAccess();
			conn = dao.getConnection();
			pstmt = conn.prepareStatement(sql);			
			rs = pstmt.executeQuery();
			while (rs.next()) 
			{
				EmailPool item = new EmailPool();
				item.setId((rs.getString(EmailPool.ID)));
				item.setBizType(rs.getString(EmailPool.BIZTYPE));
				item.setAttachmentName(rs.getString(EmailPool.ATTACHMENTNAME));
				item.setAttachmentPath(rs.getString(EmailPool.ATTACHMENTPATH));
				item.setMailContent(rs.getString(EmailPool.MAILCONTENT));
				item.setMailSendStatus(rs.getString(EmailPool.MAILSENDSTATUS));
				item.setCreateTime(rs.getDate(EmailPool.CREATETIME));
				item.setMark(rs.getString(EmailPool.MARK));
				arrData.add(item);
			}
			return arrData;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dao.close(rs,pstmt, conn);
		}
		return null;
	}

	@Override
	public boolean updateById(EmailPool ep) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		DBAccess dao = null;
		String strSql = "";
		try{
			dao = new DBAccess();
			conn = dao.getConnection();
			strSql = "update " + EmailPool.FC_EMAIL_POOL + " set "
			+ EmailPool.BIZTYPE + "='" + ep.getBizType() + "',"
			+ EmailPool.MAILSUBJECT + "='" + ep.getMailSubject()+"',"
			+ EmailPool.ATTACHMENTNAME + "='" +ep.getAttachmentName()+"',"
			+ EmailPool.ATTACHMENTPATH + "='" +ep.getAttachmentPath()+"',"
			+ EmailPool.MAILCONTENT + "='" +ep.getMailContent()+"',"
			+ EmailPool.MAILSENDSTATUS + "='" +ep.getMailSendStatus()+"',"
			//+ EmailPool.CREATETIME + "=" +ep.getCreateTime()+","
			+ EmailPool.MARK + "='" + ep.getMark() + "'"
			+ "  where " +EmailPool.ID + "='" +ep.getId() + "'";
			pstmt = conn.prepareStatement(strSql);
			if(pstmt.executeUpdate() <= 0){
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dao.close(pstmt, conn);
		}
		return true;
	}

}
